package com.topsun.framework.common.core.base;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import com.topsun.framework.common.core.enums.GlobalErrorCodeEnum;
import com.topsun.framework.common.core.util.RequestUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;

/**
 * @author: 皮皮
 * @date: 2020/9/25 3:38 PM
 */
@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult<T> implements Serializable {

    /**
     * apiresult
     */
    private static final long serialVersionUID = 1922539991804940811L;
    @ApiModelProperty("调用是否成功")
    @Builder.Default
    @JSONField(ordinal = 1)
    private boolean success = true;
    @ApiModelProperty(value = "状态码", required = true)
    @Builder.Default
    @JSONField(ordinal = 2)
    private Integer code = 200;
    @ApiModelProperty("调用结果消息")
    @Builder.Default
    @JSONField(ordinal = 3)
    private String message = "";
    @ApiModelProperty(value = "时间戳", required = true, dataType = "Long")
    @Builder.Default
    @JSONField(ordinal = 4)
    private Long timestamp = System.currentTimeMillis();
    @ApiModelProperty(value = "请求id", required = true, dataType = "String")
    @Builder.Default
    @JSONField(ordinal = 5)
    private String requestId = RequestUtil.getRequestId();
    @ApiModelProperty("成功时响应数据")
    @JSONField(ordinal = 6)
    private T data;

    /**
     * <p> 返回成功结果，无返回结果集 </p>
     *
     * @return APIResult<T>
     */
    public static <T> ApiResult<T> ok() {
        return ok(null);
    }

    /**
     * <p> 返回成功结果，有返回结果集 </p>
     *
     * @param data 返回结果集
     * @return APIResult<T>
     */
    @SuppressWarnings("unchecked")
    public static <T> ApiResult<T> ok(T data) {
        return ((ApiResult<T>) ApiResult.builder().data(data).build());
    }

    @SuppressWarnings("unchecked")
    public static <T> ApiResult<T> ok(T data, String message) {
        return ((ApiResult<T>) ApiResult.builder().data(data).message(message).build());
    }

    /**
     * <p> 返回失败结果，默认错误码与错误提示信息 </p>
     *
     * @return APIResult<T>
     */
    @SuppressWarnings("unchecked")
    public static <T> ApiResult<T> error() {
        return ((ApiResult<T>) ApiResult.builder().success(false).code(GlobalErrorCodeEnum.SYS_EXCEPTION.getCode())
                .message(GlobalErrorCodeEnum.SYS_EXCEPTION.getDesc()).data(null).build());
    }

    /**
     * <p> 返回失败结果，默认错误码，指定错误提示信息 </p>
     *
     * @param message 错误提示信息
     * @return APIResult<T>
     */
    @SuppressWarnings("unchecked")
    public static <T> ApiResult<T> error(String message) {
        return ((ApiResult<T>) ApiResult.builder().success(false).code(GlobalErrorCodeEnum.SYS_EXCEPTION.getCode())
                .message(message).data(null).build());
    }

    /**
     * <p> 返回失败结果，指定错误码与错误提示信息 </p>
     *
     * @param code
     * @param message
     * @return APIResult<T>
     */
    @SuppressWarnings("unchecked")
    public static <T> ApiResult<T> error(Integer code, String message) {
        return ((ApiResult<T>) ApiResult.builder().success(false).code(code).message(message).data(null).build());
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
